package com.one.group.admin.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.InventoryRefinementMapper;
import com.one.group.admin.mapper.InventorySummaryMapper;
import com.one.group.model.dto.InventorySummaryQueryDto;
import com.one.group.model.dto.InventorySummaryUpdateDto;
import com.one.group.model.entity.MaterialPrice;
import com.one.group.model.entity.InventoryRefinement;
import com.one.group.model.entity.InventorySummary;
import com.one.group.model.vo.InventorySummaryListVo;
import com.one.group.service.InventorySummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class InventorySummaryServiceImpl extends ServiceImpl<InventorySummaryMapper, InventorySummary> implements InventorySummaryService {
    @Autowired
    InventoryRefinementMapper inventoryRefinementMapper;
    @Override
    public List<InventorySummaryListVo> findInventorySummaryByQueryDto(InventorySummaryQueryDto inventorySummaryQueryDto) {
        return this.getMapper().findInventorySummaryByQueryDto(inventorySummaryQueryDto);
    }

    @Override
    @Transactional
    public void updateInventorySummaryByUpdateDto(InventorySummaryUpdateDto inventorySummaryUpdateDto) {
//        先修改总表
//        先检查总表是否有这个物料以及价格，没有就是插入，有就是修改
        for (MaterialPrice materialPrice : inventorySummaryUpdateDto.getMaterialPriceList()) {
            QueryWrapper queryWrapper=QueryWrapper.create().from(InventorySummary.class);
            queryWrapper.eq(InventorySummary::getMaterialId,materialPrice.getMaterialId())
                    .eq(InventorySummary::getPrice,materialPrice.getPrice());
            InventorySummary inventorySummary = this.getMapper().selectOneByQuery(queryWrapper);
            if (inventorySummary!=null){
                inventorySummary.setStocks(inventorySummary.getStocks()+materialPrice.getQuantity());
                this.getMapper().insertOrUpdateSelective(inventorySummary);

//                详细表插入
                QueryWrapper queryWrapper1=QueryWrapper.create().from(InventoryRefinement.class);
                queryWrapper1.eq(InventoryRefinement::getInventorySummaryId,inventorySummary.getId())
                        .eq(InventoryRefinement::getRepositoryId,inventorySummaryUpdateDto.getRepositoryId());
                InventoryRefinement inventoryRefinement = inventoryRefinementMapper.selectOneByQuery(queryWrapper1);
                if (inventoryRefinement!=null){
                    inventoryRefinement.setStocks(inventoryRefinement.getStocks()+materialPrice.getQuantity());
                    inventoryRefinementMapper.insertOrUpdateSelective(inventoryRefinement);
                }
                else {
                    InventoryRefinement inventoryRefinement1 = new InventoryRefinement();
                    inventoryRefinement1.setStocks(materialPrice.getQuantity());
                    inventoryRefinement1.setRepositoryId(inventorySummaryUpdateDto.getRepositoryId());
                    inventoryRefinement1.setInventorySummaryId(inventorySummary.getId());
                    inventoryRefinementMapper.insertSelective(inventoryRefinement1);
                }

            }
            else{
                InventorySummary inventorySummary1=new InventorySummary();
                inventorySummary1.setMaterialId(materialPrice.getMaterialId());
                inventorySummary1.setPrice(materialPrice.getPrice());
                inventorySummary1.setStocks(materialPrice.getQuantity());
                inventorySummary1.setOutboundPrice(materialPrice.getPrice().multiply(BigDecimal.valueOf(1.5)));
                this.getMapper().insertSelective(inventorySummary1);
//插入详细表
                InventoryRefinement inventoryRefinement1 = new InventoryRefinement();
                inventoryRefinement1.setStocks(materialPrice.getQuantity());
                inventoryRefinement1.setRepositoryId(inventorySummaryUpdateDto.getRepositoryId());
                inventoryRefinement1.setInventorySummaryId(inventorySummary1.getId());
                inventoryRefinementMapper.insertSelective(inventoryRefinement1);
            }
        }


    }

}
